<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\GoodsCollect;
use App\Models\User;
use Illuminate\Http\Request;


class UserController extends Controller
{
    // 获取用户的基本信息，用于个人中心展示和编辑
    public function data()
    {
        $user_id = auth('wechat')->id();
        $data = User::find($user_id, [
            'avatar', 'nickname', 'mobile',
            'sex', 'age', 'height',
            'weight'
        ]);
        $data->goods_collect_num = GoodsCollect::whereUserId($user_id)->count();

        return $this->writeJson(0, $data);
    }

    // 编辑资料
    public function edit(Request $request)
    {
        $model = User::find(auth('wechat')->id());
        $model->nickname = $request->post('nickname');
        $model->avatar = $request->post('avatar');
        $model->sex = $request->post('sex');
        $model->age = $request->post('age');
        $model->height = $request->post('height') ?: null;
        $model->weight = $request->post('weight') ?: null;
        $model->save();
        return $this->writeJson(0, null, '修改成功');
    }
}
